<template>
<slot :controller="controller" :store="store" :model="model" :singleSelect="singleSelect" :localMode="localMode" :rowActiveMode="rowActiveMode" :actions="{ handleCustomCallBack, handleRowClick, handleRowDbClick, handleSelectionChange, handleSortChange, handlePageChange, handlePageSizeChange, handleFilterChange, handleActionClick  }">
  <div :class="classNames" :id="controlID">
    <a-form class="control-content">
      <AppGrid
        v-bind="$attrs"
        :columns="model.columns"
        :aggData="store.aggData"
        :items="store.data"
        :singleSelect="singleSelect"
        :localMode="localMode"
        :selections="store.selections"
        :showHeader="model.showHeader"
        :current="store.curPage"
        :limit="store.limit"
        :columnFilter="store.columnFilter"
        :totalRecord="store.totalRecord"
        :context="store.context"
        :viewParams="store.viewParams"
        :enableRowEdit="model.enableRowEdit"
        :enableAgg="model.aggMode !== 'NONE'"
        :rowActiveMode="rowActiveMode"
        :customCallBack="handleCustomCallBack()"
        :paging="model.paging"
        @row-click="handleRowClick"
        @row-db-click="handleRowDbClick"
        @selection-change="handleSelectionChange"
        @add-item="() => controller.newRow()"
        @remove-item="(row: IParam, index: number) => handleRemove([row])"
        @sort-change="(field: string, dir: '' | 'asc' | 'desc') => handleSortChange(controller, field, dir)"
        @page-change="(currentPage: number) => handlePageChange(controller, currentPage)"
        @page-size-change="(pageSize: number) => handlePageSizeChange(controller, pageSize)"
        @column-filter-change="handleFilterChange"
        @action-click="(logic: any, data: IParam, event: MouseEvent) => handleActionClick(controller, logic as IViewLogicInput, data, event)"
      >
      <template v-slot:[slotName]="{ record, column, text, index, title, actions, aggData, columns, emptyText }" v-for="slotName of slotKeys">
        <slot
          :controller="controller" 
          :store="store" 
          :model="model"
          :name="slotName"
          :record="record"
          :column="column"
          :text="text"
          :title="title" 
          :index="index"
          :context="context"
          :viewParams="viewParams"
          :aggData="aggData" 
          :columns="columns"
          :emptyText="emptyText"
          :actions="actions">
        </slot>
      </template>
        <template #ibizorderdetailname="{ record, column, index, text }">
            <slot name="ibizorderdetailname" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :actions="{ handleComponentAction }">
            <template v-if="store.enableRowEdit">
                <slot name="ibizorderdetailnameRowEdit" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :context="context" :viewParams="viewParams" :actions="{ handleComponentAction }">
                <a-form-item name="ibizorderdetailname" class="grid-edit-column" :validateStatus="store.ruleModel[index] && store.ruleModel[index]['ibizorderdetailname'].error ? 'error' : 'success'" :help="store.ruleModel[index] && store.ruleModel[index]['ibizorderdetailname'].error ? store.ruleModel[index]['ibizorderdetailname'].error : null">
                <slot name="ibizorderdetailnameEditor" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :actions="{ handleComponentAction }">
                <AppInput
                    v-bind="{ editorType: 'TEXTBOX', }"
                    type="text"
                    style=""
                    name="ibizorderdetailname"
                    :isDebounce="true"
                    :value="record.ibizorderdetailname"
                    @component-action="(actionParam:any) => { handleComponentAction(controller, actionParam, index) }"
                />
                </slot>
                </a-form-item>
                </slot>
            </template>
            <template v-else>
                <slot name="ibizorderdetailnameColumn" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :context="context" :viewParams="viewParams" :actions="{ handleActionClick }">
                <AppGridColumn :text="text" :column="column" :record="record" :index="index" :context="context" :viewParams="viewParams" @handle-action="(detail:any, event:MouseEvent) => handleActionClick(controller, detail.logic, record, event)" />
                </slot>
            </template>
            </slot>
        </template>
        <template #IBIZuniproductname="{ record, column, index, text }">
            <slot name="IBIZuniproductname" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :actions="{ handleComponentAction }">
            <template v-if="store.enableRowEdit">
                <slot name="IBIZuniproductnameRowEdit" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :context="context" :viewParams="viewParams" :actions="{ handleComponentAction }">
                <a-form-item name="ibizuniproductname" class="grid-edit-column" :validateStatus="store.ruleModel[index] && store.ruleModel[index]['ibizuniproductname'].error ? 'error' : 'success'" :help="store.ruleModel[index] && store.ruleModel[index]['ibizuniproductname'].error ? store.ruleModel[index]['ibizuniproductname'].error : null">
                <slot name="IBIZuniproductnameEditor" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :actions="{ handleComponentAction }">
                <AppDropdownList
                    v-bind="{ editorType: 'DROPDOWNLIST', }"
                    :multiple="false"
                    :context="store.context"
                    :viewParams="store.viewParams"
                    style=""
                    name="ibizuniproductname"
                    :data="record"
                    :value="record.ibizuniproductname"
                    @component-action="(actionParam:any) => { handleComponentAction(controller, actionParam, index) }"
                /> 
                </slot>
                </a-form-item>
                </slot>
            </template>
            <template v-else>
                <slot name="IBIZuniproductnameColumn" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :context="context" :viewParams="viewParams" :actions="{ handleActionClick }">
                <AppGridColumn :text="text" :column="column" :record="record" :index="index" :context="context" :viewParams="viewParams" @handle-action="(detail:any, event:MouseEvent) => handleActionClick(controller, detail.logic, record, event)" />
                </slot>
            </template>
            </slot>
        </template>
        <template #quantity="{ record, column, index, text }">
            <slot name="quantity" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :actions="{ handleComponentAction }">
            <template v-if="store.enableRowEdit">
                <slot name="quantityRowEdit" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :context="context" :viewParams="viewParams" :actions="{ handleComponentAction }">
                <a-form-item name="quantity" class="grid-edit-column" :validateStatus="store.ruleModel[index] && store.ruleModel[index]['quantity'].error ? 'error' : 'success'" :help="store.ruleModel[index] && store.ruleModel[index]['quantity'].error ? store.ruleModel[index]['quantity'].error : null">
                <slot name="quantityEditor" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :actions="{ handleComponentAction }">
                <AppInput
                    v-bind="{ editorType: 'TEXTBOX', }"
                    type="text"
                    style=""
                    name="quantity"
                    :isDebounce="true"
                    :value="record.quantity"
                    @component-action="(actionParam:any) => { handleComponentAction(controller, actionParam, index) }"
                />
                </slot>
                </a-form-item>
                </slot>
            </template>
            <template v-else>
                <slot name="quantityColumn" :controller="controller" :store="store" :model="model" :record="record" :column="column" :index="index" :text="text" :context="context" :viewParams="viewParams" :actions="{ handleActionClick }">
                <AppGridColumn :text="text" :column="column" :record="record" :index="index" :context="context" :viewParams="viewParams" @handle-action="(detail:any, event:MouseEvent) => handleActionClick(controller, detail.logic, record, event)" />
                </slot>
            </template>
            </slot>
        </template>
      </AppGrid>
    </a-form>
  </div>
</slot>
</template>
<script setup lang="ts">
// 基于template/src/widgets/{{appEntities}}/{{ctrls@GRID}}-grid/{{spinalCase ctrl.codeName}}-grid-base.vue.hbs生成

import { AppGrid } from "@components/widgets/grid";
import { AppGridColumn, AppGridColumnHeader } from '@components/widgets/grid';
import { useProps, useBasic, useAction, useController } from './temp-mode-grid-grid-hook';
import { GridActionType, IGridAbility, IGridStore, IParam, IViewLogicInput } from "@/core/interface";
// 输入参数
const props = defineProps(useProps());

// 基础数据
const { controlID, slotKeys, getClassNames, model ,useNavParamsBind, useEventBind, handleActionClick, handlePageChange, handlePageSizeChange, handleSortChange, handleComponentAction } = useBasic(props);

const emit = defineEmits<{
    (event: 'ctrl-action', name: string, action: GridActionType, data: IParam[]): void;
    (event: 'ctrl-init', name: string, data: IGridAbility): void;
    (event: 'ctrl-destroy', name: string, data: IParam): void;
}>();

const evt = useEventBind(emit);
//  部件样式名
const classNames = computed(() => {
  return getClassNames(props);
});

//  表格控制器
const controller = useController(props, evt, { controlID });

useNavParamsBind(controller, props);

const store: IGridStore = controller.getStore();

const ctrlService = controller.getCtrlService();

const  { handleRowClick, handleFilterChange, handleRowDbClick, handleSelectionChange, handleCustomCallBack, handleRemove } = useAction(controller);

onMounted(() => {
    controller.ctrlMounted();
});
onUnmounted(() => {
    controller.ctrlDestroy();
})
</script>
